#include "protocol.h"

Protocol::Protocol(Protocol::Type type)
    : _type(type)
{
}

bool Protocol::isValidProtocolType(int value)
{
        constexpr int min = (int)(Protocol::Type::Login);
        constexpr int max = (int)(Protocol::Type::Login);
        return value >= min && value <= max;
}

ProtocolLogin::ProtocolLogin()
    : Protocol(Type::Login)
{}

QString ProtocolLogin::serialize()
{
    QJsonObject json;
    json["type"] = _type;
    json["username"] = username;
    json["password"] = password;

    QJsonDocument doc(json);
    return doc.toJson(QJsonDocument::Compact);
}

void ProtocolLogin::deserialize(const QString &message)
{
    QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
    if (!doc.isObject()) {
        // 处理解析错误
        return;
    }

    QJsonObject json = doc.object();
    _type = (Protocol::Type)json["type"].toInt();
    username = json["username"].toString();
    password = json["password"].toString();
}

QString ProtocolLogin::getUsername()
{
    return username;
}

QString ProtocolLogin::getPassword()
{
    return password;
}

void ProtocolLogin::setUsername(QString username)
{
    this->username = username;
}

void ProtocolLogin::setPassword(QString password)
{
    this->password = password;
}


